package ImageFile
{
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.filesystem.File;
	
    [Event(name=ProgressEvent.COMPLETE, type="flash.events.ProgressEvent")]

	public class ImageFile
	{
		private var         _file:File;
        private var         _section:String = "";
        private var         _pagenumber:String = "";
        private var         _species:String = "";
        private var         _remark:String = "";
        private var         _newfilename:String = "";

		public function ImageFile(f:File = null) {
			_file = f;
		}
		
		public function get name():String {
			return _file.name;
		}
		
        public function get section():String {
            return _section;
        }
        
        public function get url():String {
        	return _file.url;
        }

        public function get size():Number {
        	return _file.size;
        }
        
        public function set section(pStr:String):void {
            _section = pStr;     
            setNewFileName();  
        }
        
        public function get pageNumber():String {
            return _pagenumber;
        }
        
        public function set pageNumber(pStr:String):void {
            _pagenumber = pStr;            
            setNewFileName();  
        }
        public function get species():String {
            return _species;
        }
        
        public function set species(pStr:String):void {
            _species = pStr;            
            setNewFileName();  
        }

        public function get remark():String {
            return _remark;
        }
        
        public function set remark(pStr:String):void {
            _remark = pStr;            
        }

        public function get textName():String {
        	return name.replace(extension, "txt");
        }
        
        public function get newFileName():String {
            return _newfilename;
        }
        
        public function set newFileName(pstr:String):void {
            _newfilename = pstr;
        }
        
        public function get newTextName():String {
        	return _newfilename.replace(extension, "txt");
        }
        
        public function get extension():String {
        	return _file.extension;
        }
        
        public function stem():String {
        	var i:int = name.lastIndexOf(".");
        	if(i > 0) {
        		return name.substr(0, i);
        	}
        	else {
        		return name;
        	}
        }

        public function setNewFileName():void {
            if(_section == "" && _pagenumber == "" && _species == "") {
                _newfilename = "";
                return;
            }
            var ret:String = _section + _pagenumber;
            if(ret.length > 0 && species.length > 0)
                ret += "-";
            ret += _species;
            ret += ".";
            ret += extension;
            _newfilename = ret;
        }
        
        public function setPageNumber(from:String, offset:int):void {
            var mode:String = inferMode(from);
            var i:int = 0;
            switch(mode) {
                case "i":
                    i = parseInt(from, 10) + offset;
                    this.pageNumber = ("00000" + i.toString()).substr(-from.length);
                    return;
                    
                case "b":
                    i = parseInt(from,10) + (offset + 1)/2 ;
                    pageNumber = ("00000" + i.toString() + (offset % 2 === 0 ? "b" : "a")).substr(-from.length);
                    return;
                   
                case "a":
                    i = parseInt(from,10);
                    i += (offset)/2;
                    pageNumber = ("00000" + i.toString() + (offset % 2 === 0 ? "a" : "b")).substr(-from.length);
                    return;
                    
                default:
                    return;
            }
        }
        
            public function get htmlString():String {
                return "<img src='" + this.url + "' width='100%' />";
            }

        
        private static function isinteger(istr:String):Boolean {
            if(istr.length === 0) {
                return false;
            }
            var re:RegExp = /\D/;
            var r:int = istr.search(re);
            // if there are non-integers, return false
            return r === -1;
        }
    
        private static function istype2(istr:String):Boolean {
            var re:RegExp = /\d+[ab]/;
            var r:int = istr.search(re);
            return r !== -1;
        }
    
        private static function inferMode(str:String):String {
            if(isinteger(str)) {
                return 'i';
            }
            if(istype2(str)) {
                return str.substr(-1) === "a" ? "a" : "b";
            }
            return '';
        }
        
        private function get folderPath():String {
        	return _file.parent.nativePath;
        }
        
        public function get path():String {
            return _file.nativePath.toString();    
        }
        

        public static function buildPath(p1:String, p2:String):String {
        	for( ; p1.length > 0 && ( p1.substr(-1) === "\\" || p1.substr(-1) === "/"); ) {
        		p1 = p1.substr(0, p1.length-1);
        	}
        	for( ; p2.length > 0 && ( p2.substr(0, 1) === "\\" || p2.substr(0, 1) === "/"); ) {
        		p2 = p2.substr(1);
        	}
        	return p1 + "/" + p2;
        }
        
        private function zipPath():String {
            var f:File = new File(folderPath);
            var s:String = buildPath(folderPath, f.name);
            f = f.resolvePath(s);
            if(! f.exists) {
            	f.createDirectory();
            }
        	s = buildPath(s, f.name + "-page-images");
        	f = f.resolvePath(s);
            if(! f.exists) {
                f.createDirectory();
            }
        	
        	return s;
        }
        
        public function clear():void {
            remark = "";
            section = "";
            pageNumber = "";
            species = "";	
        }
        
        public function get isText():Boolean {
            var ffrom:File = new File(buildPath(folderPath, textName));
            return ffrom.exists;
        }
        
        public function get isTextY():String {
        	return isText ? "Yes": "";
        }
        
        public function copyRename(isText:Boolean):void {
        	if(newFileName === "") {
        		return;
        	}
             var toname:String = buildPath(zipPath(), newFileName);
             var fto:File = new File(toname);
             _file.copyToAsync(fto, true);
             if(isText) {
                var ffrom:File = new File(buildPath(folderPath, textName));
                if(ffrom.exists) {
                    toname = buildPath(zipPath(), newTextName);
                    fto = new File(toname)
                    ffrom.copyTo(fto, true);
                 }
             }
        }  
	}
}
